package org.itboys.core.service.mongo.web;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayArea;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.util.NumberUtil;
import org.itboys.core.util.PhoneUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/8/14
 * @Description：配送区域service
 */
@Service
public class SundayDistributionAreaService extends BaseMongoService<SundayArea>{
    @Autowired
    private SundayUploadService uploadService;
    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }
    @Override
    protected Class<SundayArea> getEntityClass() {
        return SundayArea.class;
    }

    @Autowired
    private SundayStoreService storeService;

    public PageResult<SundayArea> select(HttpServletRequest request){
        Map<String,Object> param=new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request, "name");
        PageResult<SundayArea> sundayAreaPageResult = super.containpageQuery(request, vagueParam, param);
        List<SundayArea> data =sundayAreaPageResult.getData();
        if(ListUtil.isNotNull(data)){
            List<Long> ids=new ArrayList<>();
            for (SundayArea area:data) {
                ids.add(area.getStoreId());
            }
           param.put("id in",ids);
            List<SundayStore> stores = storeService.list(param);
            for (SundayArea area:data) {
                for (SundayStore store:stores) {
                    if(store.getId()==area.getStoreId().longValue()){
                        area.setStoreName(store.getName());
                    }
                }
            }
            sundayAreaPageResult.setData(data);
        }
        return sundayAreaPageResult;
    }


    public void save(Long id,SundayArea area){
        if(!PhoneUtil.isMobileNumber(area.getMobile())){
            throw new ErrorException("手机格式错误");
        }
        if(LongUtil.isNotZreo(id)){
            super.updateExceptEmpty(id,area);
        }else{
            super.save(area);
        }
    }

    public SundayArea findOne(Long id){
        SundayArea area=new SundayArea();
        if(LongUtil.isNotZreo(id)){
            area = super.getById(id);
            Map<String,Object> param=new HashMap();
            param.put("id",area.getStoreId());
            param.put("isDeleted",SundayCommonConstants.NO);
            List<SundayStore> list = storeService.list(param);
            if (ListUtil.isNotNull(list)){
                area.setStoreName(list.get(0).getName());
            }
        }
        return area;
    }
}
